//载入那些函数库
let includes = ([
    "include/common.js",
]);

//继承哪些JS 在所有代码之前要完成继承
let inherits = ([
    "inherit/data.js",
    "inherit/room/sim.js",
    "inherit/save.js"
]);


let vars = {
    timer: 0,
    ownUser: 0,
    //事件
    events: {
        //抢劫
        qiangjie: {
            name: "抢劫",
            //事件描述
            desc: ["你小子真不走运，又被抢劫了"],
        },
        //离职
        lizhi: {
            name: "人资",
            desc: ["这儿美女太少，大家都吵扰着离开了"]
        },
        //娱乐实施
        yule: {
            name: "娱乐",
            desc: ["天天干活儿，都没有玩的地方"]
        },
        //电力
        dianli: {
            name: "电力",
            desc: ["供电不足，该想想办法了..."]
        }

        //人口

    }
};


let homes = [];
let homeData = {
    id: "",
    //等级
    lv: 1,
    //人口
    ppCount: 0,
    //满意度
    satisfaction: 10,
    type: "home",
};


//电站数据结构
let dianzhan = {
    id: "",
    //等级
    lv: 1,
    type: "dian",
};


vars.cityData = {
    //人民满意度
    ppSatis: 5,
    //总的电力
    dian: 0,
    //土地
    land: {
        //土地上的建筑
        build: [],
    }
};


/**
 * 电力消耗算法
 * 电
 * 人口 / 2
 * -----------------------------------
 * 小型风力发电站 2000  10点电力
 * 中型风力发电站 5000  50点电力
 * @returns {*}
 */



//被创建的时候触发
function create() {
    let me = thisObject;
    me.set("name", "模拟城市");
    me.set("desc", `这是一个模拟城市.....`);
    //是否是玩家
    me.set("obType", "isRoom");
    me.set("heartbeat", true);

    me.set("min", 20);
    //资源上限
    me.set("resLimit", 20);
    me.set("ownUser", "");
    me.set("timer", 0);
    me.set("res", 20);

    //事件记录....
    me.set("roomEventListLog", {});


    me.set("exits", {
        south: "d/chengdu/guangchang.js",
    });


    me.set("cmds", {
        start: start,
        //创建住宅
        cr: createHome,
        //查看
        ls: list,

    })

    return me;
}


function list(caller, arg) {
    let me = thisObject;

    os.debug(arg, "list cmd");

    if (arg == "pps") {//查看人口
        me.sendOwn("当前的总人口数量是" + me.getAllPpCount());
    } else if (arg == "homes") {

    }


}


/**
 * 100001 小型风力电站
 * 100020  1级住宅
 * @param caller
 * @param arg
 */

vars.simConf = {
    builds: {
        "100001": {
            id: 100001,
            name: "小型风力电站",
            out: 10,
            type: "dian",
        },

        "100020": {
            id: 100020,
            lv: 0,
            ppCount: 0,
            //最大人口
            maxppCount: 6,
            name: "小型住宅",
            type: "home",
        },
        "100090": {
            id: 100020,
            lv: 0,
            ppCount: 0,
            //最大人口
            maxppCount: 6,
            name: "部落中心",
            type: "home",
        }
    }
}


let randHomeName = [
    "大王小区", "凌云府", "格调兰庭", "天骏", "恒大一期"
];


function createHome(caller, arg) {
    let me = thisObject;
    let data = vars.simConf.builds[arg];

    if (data.type == "home") {
        data.name = getRandomOneOfArr(randHomeName);
    }

    vars.cityData.land.build.push(data);
    me.sendOwn("创建成功！");

}


//获取所有住宅
function getHomes() {
    let homes = vars.cityData.land.build.filter(e => {
        return e.type == "home";
    });
    return homes;
}


function getDianzhans() {
    let dianz = vars.cityData.land.build.filter(e => {
        return e.type == "dian";
    });
    return dianz;
}

//获取总的电力
function getOutDian() {
    let d = 0;
    getDianzhans().forEach(e => {
        d += e.out;
    })
    return d;
}


function getAllPpCount() {
    let count = 0;
    getHomes().forEach(e => {
        count += e.ppCount;
    })
    return count;
}


function start(caller, arg) {

    let me = thisObject;

    if (objectp(me.get("ownUser"))) {
        tellObject(caller, "知道什么叫名矿有主吗？").to();
        return;
    }

    if (!objectp(me.get("ownUser"))) {
        me.set("ownUser", caller);
        me.vars.ownUser = caller;
        tellObject(caller, "你很轻松的就获得了一个价值几百万的矿场").to();
        return;
    }
}


function sendOwn(msg) {
    let me = thisObject;
    if (objectp(me.get("ownUser"))) {
        tellObject(me.get("ownUser"), msg).to();
        return;
    }
}


function addRes(d) {
    let me = thisObject;
    let res = me.get("res");
    res += d;
    me.set("res", res);
    tellObject(me.get("ownUser"), `你的矿石增加了，当前有${res}个..`).to();
}

function getEvent() {

    let me = thisObject;
    let r = random(0, 100);
    let l = random(0, 100);
    // os.debug(r,"debuger");
    // os.debug(r,"随机数");
    if (r < 10 && l < 10) {
        let desc, eventSize = Object.keys(me.vars.events).length;
        let eventKes = Object.keys(me.vars.events);
        let k = getRandomOneOfArr(eventKes);
        tellObject(me.get("ownUser"), `系统选中了${me.vars.events[k].name}事件`).to();
        desc = getRandomOneOfArr(me.vars.events[k].desc);
        tellObject(me.get("ownUser"), `${desc}`).to();
    }
}


function heartbeat() {


    let me = thisObject;


    if (!objectp(me.get("ownUser"))) return;


    if (me.vars.timer <= 0) {
        me.vars.timer = new Date().getTime();
    }

    let diffTime = new Date().getTime() - me.vars.timer;
    diffTime = timeDuring(diffTime, "s");
    if (diffTime < 2) {
        return;
    }

    // me.save();
    me.vars.timer = new Date().getTime();
    me.getEvent();
    me.sendOwn(`时间过去了${diffTime}s`);
    // me.addRes(1);

    //随机增加人口


    let r = random(0, 100);
    let homes = getHomes();
    if (r < 50 && homes.length > 0) {
        let idx = random(0, homes.length - 1);
        if (homes[idx].ppCount < homes[idx].maxppCount) {
            homes[idx].ppCount += 1;
            me.sendOwn(homes[idx].name + "的人口增加了");
        }
    }

    let costDian = getAllPpCount() / 2;

    let allDian = getOutDian();
    if (allDian < costDian) {
        me.sendOwn("电力不足...");
    }

    me.sendOwn(`用电情况${costDian}/${allDian}`);


    return false;
}


function querySaveFile() {
    return "data/game/sim/" + thisObject.vars.ownUser.get("id");
}




